package com.thrc.borrow.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.entity.Course;
import com.thrc.borrow.entity.Order;
import com.thrc.borrow.service.CourseService;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.CourseVo;
import com.thrc.borrow.vo.OrderVo;

@Controller
@RequestMapping("/course")
public class CourseController {

	@Autowired 
	private CourseService courseService;
	
	@RequestMapping("/getById")
	public void selectById(HttpServletResponse response, Course t) {
		CourseVo vo = courseService.selectById(t);
		ResponseUtil.print(response, vo);
	}
	
	@RequestMapping("/insert")
	public void insert(HttpServletResponse response, Course t) {
		try {
			t.setCreate_time(new Date());
			courseService.insert(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/update")
	public void update(HttpServletResponse response, Course t) {
		try {
			courseService.update(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, Course t) {

		try {
			courseService.delete(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/list/page")
	public String goUserJsp() {

		return "course/courseList";
	}
	
	@RequestMapping("/selectCourseListByPage")
	public void selectByPage(HttpServletResponse response, Course t) {

		JSONObject retJson = new JSONObject();
		try {
			int totleCount = courseService.selectCount(t);
			retJson.put("total", totleCount);
			if (totleCount == 0) {
				retJson.put("data", null);
				ResponseUtil.print(response, retJson);
				return;
			}
			List<CourseVo> courseList = courseService.selectByPage(t);
			retJson.put("data", courseList);
			ResponseUtil.print(response, retJson);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/select/all")
	public void selectAll(HttpServletResponse response, Course t) {

		try {
			List<CourseVo> courseList = courseService.selectAll(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE,"操作成功",courseList);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
	
	@RequestMapping("/select/openid")
	public String selectByOpenid(HttpServletResponse response,Order t){
		try {
			List<OrderVo> orderList=courseService.selectByCondition(t);
			for (OrderVo orderVo : orderList) {
				Course ts=new Course();
				ts.setId(orderVo.getCourseId());
				CourseVo vo=courseService.selectById(ts);
				orderVo.setTitle(vo.getTitle());
				orderVo.setUrl(vo.getUrl());
			}
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE,"操作成功",orderList);
		}catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
		return null;
	}
}
